package exhi.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.entity.XMsg;
import com.stp.core.spring3.BaseSpringAction;

import exhi.entity.EntpAsse;
import exhi.entity.EntpBexhi;
import exhi.manager.EntpAsseMng;
import exhi.manager.EntpBexhiMng;

@Controller
@RequestMapping("/entp")
public class EntpAsseAct extends BaseAct {

	private static final String PATH_TABLE = "/asse-table";
	private static final String PATH_LIST = "/asse-list";
	private static final String PATH_DETAIL = "/asse-detail";
	private static final String PATH_SAVE = "/asse-save";
	private static final String PATH_DELETE = "/asse-delete";

	// 展会 自评表 B:展前自评；A：展后自评
	@RequestMapping(PATH_TABLE)
	public void table(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		model.addAttribute("code", code);

	}

	// 权限json数据
	@RequestMapping(PATH_LIST)
	@ResponseBody
	public String infoList(XGridPage page,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {

		String sql = "from EntpAsse info where 1=1";
		sql += sqlModel.getSQLChars("info.code", "=", capCode, false, "and");
		sql += sqlModel
				.getSQLChars("info.zhmc", "like", capName, false, "like");
		sql += " order by info.createtime desc";
		InfoGridRow row = new EntpAsseAct.InfoGridRow();
		XGrid grid = entpAsseMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}

	// 明细数据
	@RequestMapping(PATH_DETAIL)
	public String infoDetail(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@ModelAttribute("entpAsse") EntpAsse entpAsseInfo,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhi, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("asse", entpAsseInfo);
		model.addAttribute("info", entpBexhi);
		if ("B".equalsIgnoreCase(code))
			return "entp/asse-b-detail";
		if ("A".equalsIgnoreCase(code))
			return "entp/asse-a-detail";
		return "";
	}

	// 数据保存
	@RequestMapping(PATH_SAVE)
	@ResponseBody
	public String saveInfo(HttpServletRequest request,
			@ModelAttribute("entpAsse") EntpAsse entpAsseInfo, @ModelAttribute("entpBexhi") EntpBexhi entpBexhi,ModelMap model)
			throws IllegalStateException, IOException {
		
		entpAsseInfo.setCreatetime(new Date());		
		entpAsseInfo.setUid(entpBexhi.getId());
		entpAsseMng.save(entpAsseInfo);
		XMsg msg = new XMsg();
		String entpId = entpAsseInfo.getId();
		msg.setState(true);
		return toJson(msg);
	}

	@RequestMapping(PATH_DELETE)
	@ResponseBody
	public String infoDelete(@ModelAttribute("entpAsse") EntpAsse entpAsseInfo,
			ModelMap model) {
		XMsg msg = new XMsg();
		msg.setState(true);
		entpAsseMng.delete(entpAsseInfo);
		return toJson(msg);
	}

	@ModelAttribute("entpAsse")
	public EntpAsse getEntpSexhiInfo(
			@RequestParam(value = "asseId", required = true, defaultValue = "") String id) {
		EntpAsse entpAsse = entpAsseMng.get(id);
		if (null == entpAsse) {
			entpAsse = new EntpAsse();
		}
		return entpAsse;
	}

	class InfoGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			if (obj instanceof EntpAsse) {
				xrow = new XGridRow();
				EntpAsse info = (EntpAsse) obj;
				xrow.setId(info.getId());
				List t = new ArrayList();
				t.add("");
				t.add("<a href='asse-detail.jspx?infoId=" + info.getId()
						+ "&code=" + info.getCode() + "&asseId='>【编辑】</a>");
				xrow.setData(t);
			}
			return xrow;
		}
	}

	@ModelAttribute("entpBexhi")
	public EntpBexhi getEntpBexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpBexhi entpBexhi = entpBexhiMng.get(id);
		if (null == entpBexhi) {
			entpBexhi = new EntpBexhi();
		}
		return entpBexhi;
	}

	@Autowired
	@Qualifier("entpAsseMng")
	private EntpAsseMng entpAsseMng;

	@Autowired
	@Qualifier("entpBexhiMng")
	private EntpBexhiMng entpBexhiMng;

}
